#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <GUIComboBox.au3>

;Path to configuration file
Global $Configure = (@ScriptDir & "\Bin\Configuration\Configure.ini")
;Path to GUI Media
Global $GuiMedia = (@ScriptDir & "\Bin\Media\")
;Begin Form Create 
$Form1 = GUICreate("Media Auto Ripper", 631, 384, 189, 119)

;Setup Tabs
$Tab1 = GUICtrlCreateTab(8, 32, 617, 345)
GUICtrlSetResizing(-1, $GUI_DOCKWIDTH+$GUI_DOCKHEIGHT)

;Sheet 1
$TabSheet1 = GUICtrlCreateTabItem("Media Rip Control") ;Creates Sheet 1 and Sets it's name

;Begin Ripping Control Section
$Label2 = GUICtrlCreateLabel("DVD Ripping Control", 16, 72, 102, 17)
; Calls @ScriptDir\Bin\AutoRipActivate.exe
$Button1 = GUICtrlCreateButton("Auto Rip On", 24, 96, 75, 25, BitOR($BS_PUSHLIKE,$WS_GROUP))
GUICtrlSetTip(-1, "Enables the Automated DVD Ripping Proccess")

; Calls @ScriptDir\Bin\AutoRipDisable.exe
$Button2 = GUICtrlCreateButton("Auto Rip Off", 24, 128, 75, 25, $WS_GROUP)
GUICtrlSetTip(-1, "Disables Auto Ripping Feature")

;DVD Drive Select writes selection to @ScriptDir\configure.ini section1 key=
Local $DVDDriveSelect = IniRead($Configure,"DVD Drive","Key","Default")
If $DVDDriveSelect = "" Then
		$Select = GUICtrlCreateCombo("Select", 24, 184, 65, 25)
		GUICtrlSetData(-1, "D:|E:|F:|G:|H:|I:|J:|K:|L:|M:|N:|O:|P:|Q:|R:|S:|T:|U:|V:|W:|X:|Y:|Z:")
		GUICtrlSetTip(-1, "The source DVD drive.")
Else
	$Select = GUICtrlCreateCombo($DVDDriveSelect, 24, 184, 65, 25)
		GUICtrlSetData(-1, "D:|E:|F:|G:|H:|I:|J:|K:|L:|M:|N:|O:|P:|Q:|R:|S:|T:|U:|V:|W:|X:|Y:|Z:")
		GUICtrlSetTip(-1, "The source DVD drive.")		
EndIf

$Label3 = GUICtrlCreateLabel("DVD Ripping Drive", 16, 160, 94, 17)

; need to create a browse filesystem for user to select folder and write result to @ScriptDir\configure.ini section2 key=
$Label4 = GUICtrlCreateLabel("DVD Target Directory", 16, 216, 106, 17)

;File System Broswer for Target Path
$BrowseFileSystem = GUICtrlCreateButton("Browse", 536, 232, 75, 25, $WS_GROUP)
Local $BrowseFileSystemSelect = IniRead($Configure,"DVD Rip Output Path","Key", "Default")
	If $BrowseFileSystemSelect = "" Then
		$RipTargetPath = GUICtrlCreateInput("Browse to Select", 16, 232, 505, 21,$ES_READONLY)
	Else
		$RipTargetPath = GUICtrlCreateInput($BrowseFileSystemSelect & "\", 16, 232, 505, 21,$ES_READONLY)
	EndIf

;GUI on/off LEDs
Global $ActiveLedStatus = iniread($Configure, "AutoRipActivate", "Key", "default")
;Off Case
If $ActiveLedStatus = "Off" Then $ActiveLed = GUICtrlCreatePic(@ScriptDir & "\Bin\Media\Off.jpg", 112, 96, 20, 20)
If $ActiveLedStatus = "Off" Then $DeactiveLed = GUICtrlCreatePic(@ScriptDir & "\Bin\Media\On.jpg", 112, 128, 20, 20)
;On Case
If $ActiveLedStatus = "On" Then $ActiveLed = GUICtrlCreatePic(@ScriptDir & "\Bin\Media\On.jpg", 112, 96, 20, 20)
If $ActiveLedStatus = "On" Then $DeactiveLed = GUICtrlCreatePic(@ScriptDir & "\Bin\Media\Off.jpg", 112, 128, 20, 20)

;Where the completed transcodes are to be stored
;browse button for input box
$CompletedTransButton = GUICtrlCreateButton("Browse", 536, 280, 75, 25, $WS_GROUP)
	Local $CompTransSel = IniRead($Configure,"Transcode Path","Key","Default") 
		If $CompTransSel = "" Then
			$CompletedTranscode = GUICtrlCreateInput("Browse to Select", 16, 280, 505, 21,$ES_READONLY)
		Else
			$CompletedTranscode = GUICtrlCreateInput($CompTransSel & "\", 16, 280, 505, 21,$ES_READONLY)
		EndIf
;Completed Transcode Label
$Label5 = GUICtrlCreateLabel("Completed Transcode Directory", 16, 264, 153, 17)

;Sheet 2
$TabSheet2 = GUICtrlCreateTabItem("Transcode Options")
GUICtrlSetState(-1,$GUI_SHOW)
;Transcode select type
$TransCodeActionTypeLabel = GUICtrlCreateLabel("Select the Transcode Action", 16, 56, 139, 17)
$CurrTransCodeActionTypeComboSelect = "" ;Used to determine if combo box is changed
$TransCodeActionTypeCombo = GUICtrlCreateCombo("", 16, 80, 169, 25)
	GUICtrlSetData(-1, "Transcode VIDEO TS|Transcode Video File|Batch Transcode VIDEO TS|Batch Transcode Files")
	_GUICtrlComboBox_SetEditText($TransCodeActionTypeCombo, "Select")
$TransCodeActionTypeHelp = GUICtrlCreateButton("?", 158, 56, 27, 17, $WS_GROUP)	
;Encoding preset dropdown box and label
$EncodePresetLabel = GUICtrlCreateLabel("Encoding Preset", 24, 256, 82, 17)
	GUICtrlSetState(-1, $GUI_HIDE)
$EncodePresetCombo = GUICtrlCreateCombo("Select", 24, 280, 105, 25)
	GUICtrlSetData(-1, "iPhone/iPod Touch|iPod Clasic/Nano|AppleTV|Normal|Custom")
	GUICtrlSetState(-1, $GUI_HIDE)
$EncodePresetHelp = GUICtrlCreateButton("?", 105, 254, 27, 17, $WS_GROUP)
	GUICtrlSetState(-1, $GUI_HIDE)
;Video type dropdown box and label
$VideoTypeLabel = GUICtrlCreateLabel("Video", 152, 256, 31, 17)
	GUICtrlSetState(-1, $GUI_HIDE)
$VideoTypeCombo = GUICtrlCreateCombo("Combo2", 152, 280, 105, 25)
	GUICtrlSetState(-1, $GUI_HIDE)
$VideoTypeHelp = GUICtrlCreateButton("?", 185, 254, 27, 17, $WS_GROUP)
	GUICtrlSetState(-1, $GUI_HIDE)
;Audio type dropdown box and label
$AudioTypeLabel = GUICtrlCreateLabel("Audio", 280, 256, 31, 17)
	GUICtrlSetState(-1, $GUI_HIDE)
$AudioTypeCombo = GUICtrlCreateCombo("Combo3", 280, 280, 105, 25)
	GUICtrlSetState(-1, $GUI_HIDE)
$AudioTypeHelp = GUICtrlCreateButton("?", 313, 254, 27, 17, $WS_GROUP)
	GUICtrlSetState(-1, $GUI_HIDE)
;Container type dropdown box and label
$ContainerTypeLabel = GUICtrlCreateLabel("Container", 408, 256, 49, 17)
	GUICtrlSetState(-1, $GUI_HIDE)
$ContainerTypeCombo = GUICtrlCreateCombo("Combo4", 408, 280, 105, 25)
	GUICtrlSetState(-1, $GUI_HIDE)
$ContainerTypeHelp = GUICtrlCreateButton("?", 465, 254, 27, 17, $WS_GROUP)
	GUICtrlSetState(-1, $GUI_HIDE)
;Passing custom switches to handbrake
$CustomEncodeLabel = GUICtrlCreateLabel("Custom Encoding Options", 24, 320, 126, 17)
	GUICtrlSetState(-1, $GUI_HIDE)
$CustomEncodeInput = GUICtrlCreateInput("CompleteCustom", 24, 344, 489, 21)
	GUICtrlSetState(-1, $GUI_HIDE)
$CustomEncodeHelp = GUICtrlCreateButton("?", 152, 320, 27, 17, $WS_GROUP)
	GUICtrlSetState(-1, $GUI_HIDE)
;Movie Selection
$MovieSelectionGroup = GUICtrlCreateGroup("Movie Selection", 424, 64, 185, 185)
	GUICtrlCreateGroup("", -99, -99, 1, 1)
	GUICtrlSetState(-1, $GUI_HIDE)
;Encode only selected movies
$EncodeSelected = GUICtrlCreateButton("Encode Selected", 520, 280, 91, 25, $WS_GROUP)
	GUICtrlSetState(-1, $GUI_HIDE)
;Encode all movies
$EncodeAll = GUICtrlCreateButton("Encode All", 520, 312, 91, 25, $WS_GROUP)
	GUICtrlSetState(-1, $GUI_HIDE)
;Delete all movies in rip folder
$DeleteRipDir = GUICtrlCreateButton("Empty Rip  Folder", 520, 344, 91, 25, $WS_GROUP)
	GUICtrlSetState(-1, $GUI_HIDE)

GUISetState()


;Sheet 4
$TabSheet4 = GUICtrlCreateTabItem("Metadata Control")

;Sheet 3
$TabSheet3 = GUICtrlCreateTabItem("About")
GUICtrlCreateTabItem("")

;extra
$Label1 = GUICtrlCreateLabel("Media Auto Ripper", 8, 0, 171, 28)
GUICtrlSetFont(-1, 16, 800, 2, "Times New Roman")
GUISetState(@SW_SHOW)




While 1
	$nMsg = GUIGetMsg()
		Select
			;Check  if user clicked close button
			Case $nMsg = $GUI_EVENT_CLOSE
			GUIDelete()
			Exit
			;Activates Auto Rip
			Case $nMsg = $Button1
				IniWrite($Configure,"AutoRipActivate","Key","On")
				;MsgBox(64, "Debug", "Activate Was Pressed")
				GUICtrlSetImage($ActiveLed,@ScriptDir & "\Bin\Media\On.jpg")
				GUICtrlSetImage($DeactiveLed,@ScriptDir & "\Bin\Media\Off.jpg")
			;Deactivates Auto Rip	
			Case $nMsg = $Button2
				IniWrite($Configure,"AutoRipActivate","Key","Off")
				;MsgBox(64, "Debug", "Deactivate Was Pressed")
				GUICtrlSetImage($ActiveLed,@ScriptDir & "\Bin\Media\Off.jpg")
				GUICtrlSetImage($DeactiveLed,@ScriptDir & "\Bin\Media\On.jpg")
			Case $nMsg = $Select
				$DVDDriveSelect = GUICtrlRead($Select)
				IniWrite($Configure,"DVD Drive","Key",$DVDDriveSelect)
			Case $nMsg = $BrowseFileSystem
				; Get the folder to use
				$BrowseSelect = FileSelectFolder("Select the folder where you want the DVD Rips stored.", "",1)
				; Save it to the ini file
				IniWrite($Configure,"DVD Rip Output Path","Key",$BrowseSelect)
				; Change the input box text
				GUICtrlSetData($RipTargetPath, $BrowseSelect) ; Note syntax
				; Not sure if you need this line, it depends on whether you want this variable set to the new  folder
				Local $BrowseFileSystemSelect = $BrowseSelect
			;Transcode Path Button
			Case $nMsg = $CompletedTransButton
				;Get the folder to use
				$BrowseTransSelect = FileSelectFolder("Select the folder where you want the completed transcodes stored.", "",1)
				;Save it to the ini File
				IniWrite($Configure,"Transcode Path","Key",$BrowseTransSelect)
				;Change inputboxtext
				GUICtrlSetData($CompletedTranscode, $BrowseTransSelect)
			Case $nMsg = $EncodePresetCombo
				$EncodePresetComboSelect = GUICtrlRead($EncodePresetCombo)
				iniwrite($Configure,"Encode Preset","Key",$EncodePresetComboSelect)
EndSelect
			
;$TransCodeActionTypeCombo
Switch GUIGetMsg()
	Case $GUI_EVENT_CLOSE
		Exit
EndSwitch
$TransCodeActionTypeComboSelect = GUICtrlRead($TransCodeActionTypeCombo)
	If $TransCodeActionTypeComboSelect <> $CurrTransCodeActionTypeComboSelect And _GUICtrlComboBox_GetDroppedState($TransCodeActionTypeCombo) = False Then
		$CurrTransCodeActionTypeComboSelect = $TransCodeActionTypeComboSelect
		
		Switch $TransCodeActionTypeComboSelect
			Case "Transcode VIDEO TS"
				GUICtrlSetState($DeleteRipDir, $GUI_SHOW)
				GUICtrlSetState($EncodeAll, $GUI_SHOW)
				GUICtrlSetState($EncodeSelected, $GUI_SHOW)
				GUICtrlSetState($MovieSelectionGroup, $GUI_SHOW)
				GUICtrlSetState($CustomEncodeHelp, $GUI_SHOW)
				GUICtrlSetState($CustomEncodeInput, $GUI_SHOW)
				GUICtrlSetState($CustomEncodeLabel, $GUI_SHOW)
				GUICtrlSetState($ContainerTypeHelp, $GUI_SHOW)
				GUICtrlSetState($ContainerTypeCombo, $GUI_SHOW)
				GUICtrlSetState($ContainerTypeLabel, $GUI_SHOW)
				GUICtrlSetState($AudioTypeHelp, $GUI_SHOW)
				GUICtrlSetState($AudioTypeCombo, $GUI_SHOW)
				GUICtrlSetState($AudioTypeLabel, $GUI_SHOW)
				GUICtrlSetState($VideoTypeHelp, $GUI_SHOW)
				GUICtrlSetState($VideoTypeCombo, $GUI_SHOW)
				GUICtrlSetState($VideoTypeLabel, $GUI_SHOW)
				GUICtrlSetState($EncodePresetHelp, $GUI_SHOW)
				GUICtrlSetState($EncodePresetCombo, $GUI_SHOW)
				GUICtrlSetState($EncodePresetLabel, $GUI_SHOW)
			Case "Transcode Video File" 
				GUICtrlSetState($DeleteRipDir, $GUI_SHOW)
				GUICtrlSetState($EncodeAll, $GUI_HIDE)
				GUICtrlSetState($EncodeSelected, $GUI_SHOW)
				GUICtrlSetState($MovieSelectionGroup, $GUI_HIDE)
				GUICtrlSetState($CustomEncodeHelp, $GUI_HIDE)
				GUICtrlSetState($CustomEncodeInput, $GUI_HIDE)
				GUICtrlSetState($CustomEncodeLabel, $GUI_HIDE)
				GUICtrlSetState($ContainerTypeHelp, $GUI_HIDE)
				GUICtrlSetState($ContainerTypeCombo, $GUI_HIDE)
				GUICtrlSetState($ContainerTypeLabel, $GUI_HIDE)
				GUICtrlSetState($AudioTypeHelp, $GUI_HIDE)
				GUICtrlSetState($AudioTypeCombo, $GUI_HIDE)
				GUICtrlSetState($AudioTypeLabel, $GUI_HIDE)
				GUICtrlSetState($VideoTypeHelp, $GUI_HIDE)
				GUICtrlSetState($VideoTypeCombo, $GUI_HIDE)
				GUICtrlSetState($VideoTypeLabel, $GUI_HIDE)
				GUICtrlSetState($EncodePresetHelp, $GUI_HIDE)
				GUICtrlSetState($EncodePresetCombo, $GUI_HIDE)
				GUICtrlSetState($EncodePresetLabel, $GUI_HIDE)
		EndSwitch
	EndIf
WEnd
